# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   selenium_self
# FileName:      read_local_ini.py
# Author:       test_liu
# Datetime:     2024/10/14 下午8:19
# Description: 读取系统配置文件
# ---------------------------------------------------------------------------
import os
from configparser import ConfigParser
from WebTest3.WebTest import log


class ReadLocalIni:
    def __init__(self):
        self.main_path = os.path.dirname(os.path.dirname(__file__))
        date_config_path = os.path.join(self.main_path, 'data_config')
        self.local_path = os.path.join(date_config_path, 'local_config')
        local_ini_path = os.path.join(self.local_path, 'config.ini')
        self.config = ConfigParser()
        self.config.read(local_ini_path, encoding='utf-8')

    def get_db(self, key):
        try:
            db = self.config.get('sql', key)
            return db
        except Exception as e:
            log.error(f'方法‘get_db’读取配置文件失败，请检查传入的值是否存在于配置文件中，错误为{e}')
            raise e

    def get_driver_path(self, key):
        try:
            driver = self.config.get('driver', key)
            path = os.path.join(self.main_path, 'drivers', driver)
            return path
        except Exception as e:
            log.error(f'方法‘get_driver_path’读取配置文件失败，请检查传入的值是否存在于配置文件中，错误为{e}')
            raise e

    def get_url(self, key):
        try:
            url = self.config.get('url', key)
            return url
        except Exception as e:
            log.error(f'方法‘get_url’读取配置文件失败，请检查传入的值是否存在于配置文件中，错误为{e}')
            raise e

    def get_report(self, key):
        try:
            # 获取report的文件路径
            report = self.config.get('report', key)
            path = os.path.join(self.main_path, 'report', report)
            # 如果不存在，则创建一个文件夹
            if not os.path.exists(path):
                os.makedirs(path)
            return path
        except Exception as e:
            log.error(f'方法‘get_report’读取配置文件失败，请检查传入的值是否存在于配置文件中，错误为{e}')
            raise e

    def get_yaml(self, key):
        try:
            yaml = self.config.get('yaml', key)
            path = os.path.join(self.local_path, yaml)
            return path
        except Exception as e:
            log.error(f'方法‘get_yaml’读取配置文件失败，请检查传入的值是否存在于配置文件中，错误为{e}')
            raise e


if __name__ == '__main__':
    read_local_ini = ReadLocalIni()
    print(read_local_ini.get_driver_path(key='edg1e'))

